<!-- 医院端配送准入列表页-->
<template>
  <div class="distribution-admittance-box">
    <el-container class="template-box">
      <!-- tab搜索 -->
      <tabs
        class="tabs-part"
        :active-index="tabIndex"
        :tabs-arr="indexTabsArr"
        @returnIndex="tabsHandle" />
      <!-- 搜索 -->
      <el-header height="auto">
        <SearchForm
          label-width="90px"
          :data="indexSearchData"
          @searchForm="searchForm"
          @resetForm="resetForm"
          :submit-loading="submitLoading"
        />
      </el-header>
      <!-- 列表 -->
      <el-editable
        v-loading="tableLoading"
        :data="dataList"
        height="400"
        :columns="indexTableData">
        <!-- 证照提示 -->
        <template
          slot="default-supplierName"
          slot-scope="scope">
          <span>
            <el-tooltip
              v-if="scope.row.isNoExpire === 'Y'"
              effect="dark"
              :content="`有证照过期：${scope.row.supplierName}`"
              placement="top">
              <div
                class="custom-oneline"
                :style="{color: 'red'}">{{ scope.row.supplierName }}</div>
            </el-tooltip>
            <custom-tooltip
              v-else
              :value="scope.row.supplierName"/>
          </span>
        </template>
        <!-- 审核历史 -->
        <template
          slot="default-history"
          slot-scope="scope">
          <el-button
            size="small"
            @click.stop="lookHistory(scope.row)"
            type="text text-primary">查看</el-button>
        </template>
        <!-- 操作 -->
        <template
          slot="default-action"
          slot-scope="scope">
          <el-button
            v-if="scope.row.authState===2"
            size="small"
            @click.stop="toExamine(scope.row)"
            type="text text-primary">审核</el-button>
          <!--          <el-button-->
          <!--            size="small"-->
          <!--            v-if="scope.row.authState===3"-->
          <!--            @click.stop="toStop(scope.row)"-->
          <!--            type="text text-primary">终止</el-button>-->
          <el-button
            size="small"
            @click.stop="toDetails(scope.row)"
            type="text text-primary">详情</el-button>
        </template>
      </el-editable>
      <!-- 分页模块 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="curPage"
        :page-sizes="PAGE_NUMBER"
        :page-size="pageSize"
        :layout="PAGINATION_LAYOUT"
        :total="totalElements"
      />
      <el-dialog
        title="审核历史"
        :visible.sync="isShowHistory">
        <el-editable
          v-loading="historyLoading"
          :data="historyDataList"
          height="400"
          :columns="historyTableData"
        />
        <span
          slot="footer"
          class="dialog-footer">
          <el-button
            type="primary"
            @click="isShowHistory = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-container>
  </div>
</template>
<script>
import {
  indexSearchData,
  indexTableData,
  indexTabsArr,
  historyTableData
} from './const'
import { hasIn, isObject } from 'lodash'
import SearchForm from '@components/businesscomponents/search'
import ElEditable from '@components/el-editable'
import CustomTooltip from '@components/custom-tooltip'
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common'
import tabs from '@components/tabs/index.vue'
import { findAuthSupplierHospitalList, authSupplierHospitalSelectcAcAuditRecord, authSupplierHospitalAuditingByHospital } from '@const/beforeSettleApi'

import { loadavg } from 'os'
export default {
  components: { ElEditable, SearchForm, tabs, CustomTooltip },
  data () {
    return {
      indexSearchData, // 搜索标题字段
      searchData: {}, // 搜索返回内容
      tableLoading: false, // 表格loading
      dataList: [], // 列表数据
      indexTableData, // 列表表头
      indexTabsArr, // tab字段
      submitLoading: false,

      isShowHistory: false, // 是否显示审核历史
      historyLoading: false, // 审核历史loading
      historyTableData, // 审核历史表头
      historyDataList: [], // 审核历史数据

      curPage: 1, // 当前页
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      pageSize: PAGE_NUMBER[0], // 一页的数量
      totalElements: 0, // 总共的条数
      tabIndex: 4,
      authState: -2
    }
  },
  created () {
    this.getTableList()
  },
  methods: {
    // 获取列表
    getTableList () {
      this.tableLoading = true
      this.$axios({
        method: 'post',
        url: findAuthSupplierHospitalList,
        data: {
          type: 1, // 院端传1 配送商 2
          supplierName: this.searchData.supplierName ? this.searchData.supplierName : null, // supplierName  查供应商
          beginDate: this.searchData.beginDate ? this.searchData.beginDate : null,
          endDate: this.searchData.endDate ? this.searchData.endDate : null,
          authState: this.authState,
          page: this.curPage,
          size: this.pageSize,
          enterpriseId: this.$store.state.companyId
        }
      }).then(res => {
        if (hasIn(res, ['data', 'payload']) && isObject(res.data.payload)) {
          this.dataList = res.data.payload.authSupplierHospitalListByParam.content || []
          this.totalElements = res.data.payload.totalElements || 0
        } else {
          this.dataList = []
          this.totalElements = 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    //   查看审核历史
    lookHistory (row) {
      this.isShowHistory = true
      this.historyLoading = true
      this.$axios({
        method: 'get',
        url: authSupplierHospitalSelectcAcAuditRecord,
        params: {
          authId: row.id,
          authType: 1
        }
      }).then(res => {
        if (hasIn(res, ['data', 'payload'])) {
          this.historyDataList = res.data.payload || []
          this.historyLoading = false
        }
      }).catch(() => {
        this.isShowHistory = false
        this.historyLoading = false
      })
    },
    // 终止审核 调后台的接口
    stopAudit (row) {
      this.$axios({
        method: 'put',
        url: authSupplierHospitalAuditingByHospital,
        data: {
          id: row.id,
          authState: 4,
          updatedBy: this.$store.state.realName
        }
      }).then(res => {
        if (res.data.code === '0') {
          this.$message({
            type: 'success',
            message: '终止成功'
          })
          this.getTableList()
        }
      })
    },
    //   终止
    toStop (row) {
      this.$confirm('确定终止吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.stopAudit(row)
      }).catch(() => {
      })
    },
    // tabs切换处理事件
    tabsHandle (index) {
      this.authState = this.indexTabsArr[index].val
      this.curPage = 1
      this.getTableList()
    },
    // 跳转详情
    toDetails (row) {
      console.log(row, 'row')
      sessionStorage.setItem('detailInfo', JSON.stringify(row))
      this.$router.push({
        path: '/hospitalSide/channelManagement/distributionAdmittance/detail',
        query: {
          id: row.id,
          hospitalId: row.enterpriseId,
          distributorId: row.supplierId,
          authState: row.authState
        }
      })
    },
    // 去审核
    toExamine (row) {
      sessionStorage.setItem('detailInfo', JSON.stringify(row))
      this.$router.push({
        path: '/hospitalSide/channelManagement/distributionAdmittance/examine',
        query: {
          id: row.id,
          hospitalId: row.enterpriseId,
          distributorId: row.supplierId,
          authState: 2 // 审核是可以修改院内编码
        }
      })
    },
    // 搜索功能-点击查询
    searchForm (condition) {
      this.curPage = 1
      this.searchData = condition
      this.getTableList()
    },
    // 点击重置
    resetForm (condition) {
      this.curPage = 1
      this.searchData = condition
      this.getTableList()
    },
    // 页码更改
    handleSizeChange (size) {
      this.pageSize = size
      this.curPage = 1
      this.getTableList()
    },
    // 切换不一样的页面,重置一切数据
    handleCurrentChange (curPage) {
      this.curPage = curPage
      this.getTableList()
    }
  }
}
</script>
<style lang="less">
.custom-oneline {
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  // 下面两个有区别
  // word-wrap: break-word;
  word-break: break-all;

  /* autoprefixer: ignore next */
  -webkit-box-orient: vertical;
  cursor: pointer;
}
.distribution-admittance-box {
  .template-box {
    height: 100%;
    overflow: auto;
    .pagination {
      padding-top: 10px;
      display: flex;
      justify-content: flex-end;
      padding-right: 0;
    }
  }
}
</style>
